$import("com.joypard.platform.security.role.roleusers.RoleUsersView");
$import("com.joypard.platform.security.role.roleusers.RoleUsersService");

/**
 * 角色管理-控制器
 */
K.defCtrl("com.joypard.platform.security.role.roleusers.RoleUsersCtrl", {

	// 初始化
	initialize : function() {
		this.view = RoleUsersView.instance({
			roleUsersCtrl : this
		});
		this.service = RoleUsersService.instance();
		this.initEvents();
		this.initListeners();
	},

	// 初始化事件
	initEvents : function() {
		E.addEvents("getUsersByRoleIdSuccessEvent", "assignUsersSuccessEvent", "evictRolesSuccessEvent");
	},

	// 视图层监听事件
	initListeners : function() {

		E.addListener("evictRolesEvent", this.onEvictRoles, this);

		E.addListener("getUsersByRoleIdEvent", this.onGetUsersByRoleId, this);
		E.addListener("assignUserEvent", this.onAssignUser, this);
	},

	/** ------ ------ ------ ------ 事件/监听 ------ ------ ------ ------ */

	/**
	 * 根据角色id查询用户
	 */
	onGetUsersByRoleId : function(params) {
		var input = K.input({
			params : params,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("getUsersByRoleIdSuccessEvent", output.getParameter("users"));
				}
			}
		});
		this.service.getUsersByRoleId(input);
	},

	/**
	 * 为角色分配用户
	 */
	onAssignUser : function(params) {
		var input = K.input({
			params : params,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("assignUsersSuccessEvent", output);
				}
			}
		});
		this.service.assignUser(input);
	},

	/**
	 * 为角色移除用户
	 */
	onEvictRoles : function(params) {
		var input = K.input({
			params : params,
			success : function(text) {
				var output = K.createOutput(text);
				if (!output.showError()) {
					E.fireEvent("evictRolesSuccessEvent", output);
				}
			}
		});
		this.service.evictRoles(input);
	}
})